package de.jmda.mview.fx.node.typeshape.relation;

import java.util.Collection;
import java.util.LinkedList;

import de.jmda.fx.node.ArrowOrthogonal;
import de.jmda.mproc.RelationInfoCalculator.RelationInfoIsA;
import de.jmda.mview.fx.node.typeshape.Typeshape;

/**
 * "Isa" relations always start from a type's shape and end at a type's shape.
 *
 * @author rwegner
 */
public class RelationIsA
    extends Relation
{
	public RelationIsA(
			RelationInfoIsA relationInfo,
			Typeshape source,
			Typeshape target,
			Orthogonality orthogonality)
	{
		super(
				new RelationEndpointSourceTypeBased(source),
				new RelationEndpointTargetIs(target));
		
		// TODO store type shapes?

		this.orthogonality = orthogonality;
	}

	public RelationIsA(
			RelationInfoIsA relationInfo, Typeshape source, Typeshape target)
	{
		this(relationInfo, source, target, Orthogonality.OFF);
	}

	@Override
	public Collection<RelationSegment> getRelationSegments()
	{
		if (orthogonality == Orthogonality.OFF)
		{
			return super.getRelationSegments();
		}
		
		if (relationSegments == null)
		{
			// lazy initialisation
			relationSegments = new LinkedList<RelationSegment>();
		}
		
		ArrowOrthogonal arrowSource = source.getArrow();
		ArrowOrthogonal arrowTarget = target.getArrow();

		RelationSegment relationSegmentSourceTailToPoint =
				new RelationSegmentVertical(
						arrowSource.tailProperty(), arrowTarget.tailProperty());
		RelationSegment relationSegmentPointToTargetTail =
				new RelationSegmentHorizontal(
						arrowTarget.tailProperty(), arrowSource.tailProperty());

		relationSegmentSourceTailToPoint.activateHighlighting();
		relationSegmentSourceTailToPoint.activateDragging();

		relationSegmentPointToTargetTail.activateHighlighting();
		relationSegmentPointToTargetTail.activateDragging();

		relationSegments.add(relationSegmentSourceTailToPoint);
		relationSegments.add(relationSegmentPointToTargetTail);

		return relationSegments;
	}
}